<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
	var classid = 0;
	var tables = $('#dataTables').DataTable({
		"sAjaxSource" : "{{ urlfor "ProductsController.DataGrid"}}"
		,"bLengthChange": false
		,"fnServerData" : function(sSource, aoData, fnCallback) {
			var params = checkQueryTableData(aoData)
			$('#search_key').val() ? params.filter = $('#search_key').val() : ''
			params.Status = 1;
			if(classid > 0){
				params.ClassId = classid;
			}
			$.post(sSource, JSON.stringify(params),function(resp) {
				fnCallback(resp);
			},'json')
		}
		,columns: [
			{"data" : "Id", "bSortable": false}
			,{"data" : "Identifier", "bSortable": false}
			,{"data" : "FullName", "bSortable": false}
            ,{"data":"Class",render:function(data, type, full, meta) {
                    return full.ClassName;
            }}
            ,{"data":"Brand",render:function(data, type, full, meta) {
                    return full.BrandName;
             }}
			,{"data" : "ModelNumber", "bSortable": false}
			,{"data" : "ColourNumber", "bSortable": false}
			,{"data" : "SuggestedPrice", "bSortable": false}
			,{"data" : "CostPrice", "bSortable": false}
			//,{"data" : "TradePrice", "bSortable": false}
			 ,{
				"mDataProp": "Action", 
				"bSortable": false, 
				"sWidth":"10%",
				"render": function ( data, type, full, meta ) { 
					btns = '<a href="javascript:;" class="am-btn am-btn-success am-btn-xs" onclick="return selectProducts('+ meta.row +');">选择</a>';
					return btns;  
				}  
			 } 
		]
		,"aLengthMenu":[20, 50, 100, 200]
		,'bLengthChange': true
		,"serverSide": true
		,"bPaginate" : true
		,"bFilter" : false	//是否启动过滤、搜索功能 
		,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
		,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示  
		  
	});
	
	$("#searchsubmit").bind("submit", function () { //点击按钮 触发table重新请求服务器
		$("#dataTables").dataTable().fnDraw(false);
		return false;
	});
	
	$('#dataTables tbody').on('dblclick','tr', function () {
		var i = tables.row(this).index();
		selectProducts(i);
	});
	
	function selectProducts(d) {
		var data = tables.row(d).data();
		if(checkIsSetGlassesNumber(data.ClassId)) {
			var index = parent.layer.getFrameIndex(window.name);
			parent.setGlassesNumber(index, data,data.ClassId);
			parent.layer.close(index);
			return false;
		}

		data.isProduct = 1;
		parent.receiveFormData(data);
		parent.layer.closeAll('iframe');
		return false;
	}
	function selectClassToProduct(id) 
	{
		classid = id;
		$("#dataTables").dataTable().fnDraw(false);
	}
	//分类加载
	$.post('{{ urlfor "ErpClassController.DataGrid"}}', function(j) {
		var product_class = $('#product_class');
		if(j.aaData.length > 0) {
			var li = '';
			$.each(j.aaData, function(index, value){
				li += '<li><a href="javascript:;" class="am-text-primary" data-classid="'+ value.Id +'" onclick="selectClassToProduct('+ value.Id +');return false">'+ value.Prefix + value.ClassName +'</a></li>';
			});	
			product_class.html(li);
			pageScrollForPorductClass();
		}
	}, 'json');
	function pageScrollForPorductClass() {
		var windowHeight = $(window).height();
		$('#product_class').slimscroll({
			height: windowHeight - 60,
			color:'#0e90d2',
			alwaysVisible: true,
		});
	}
</script>